package com.zsh.wanandroid.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.dxq.basic_ui_library.loadSirCallBack.EmptyCallback;
import com.dxq.basic_ui_library.loadSirCallBack.LoadingCallback;
import com.zsh.wanandroid.R;
import com.zsh.wanandroid.base.BaseFragment;
import com.zsh.wanandroid.base.BasePagerFragment;

import java.util.List;

/**
 * @author zsh
 * @date 2020/3/12.
 * 功能：体系fragment
 */

public class SystemFragment extends BaseFragment {

    private TabLayout tabLayout;
    private ViewPager viewPager;

    private SparseArray<BasePagerFragment> tabFragmentSparseArray = new SparseArray<>();

    private String[] systemArr = {"体系", "导航"};

    //创建SystemFragment
    public static SystemFragment newInstance(Bundle bundle) {
        SystemFragment fragment = new SystemFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (null != arguments) {

        }
    }

    @Override
    protected int onCreateFragmentView() {
        return R.layout.fragment_common_tab;
    }

    @Override
    protected void initView(View view) {
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
    }

    /**
     * 获取接口数据
     */
    @Override
    protected void loadNet() {
        getTabLayoutData();
    }

    @Override
    protected void onNetReload(View v) {
        mBaseLoadService.showCallback(LoadingCallback.class);
        loadNet();
    }

    /**
     * 接口获取体系分类
     */
    private void getTabLayoutData() {
        mBaseLoadService.showSuccess();
        //初始化设置tabLayout
        initTabLayout(systemArr);
    }

    private void initTabLayout(String[] data) {
        for (int i = 0; i < data.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(data[i]));
        }

        initViewPager(data);

        //tabLayout监听事件
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //添加选中Tab的逻辑
                viewPager.setCurrentItem(tab.getPosition(), false);//改变viewpager
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //添加未选中Tab的逻辑
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //再次选中tab的逻辑
            }
        });
    }

    private void initViewPager(final String[] data) {
        //viewPager设置adapter当fragment嵌套fragment时，必须使用getChildFragmentManager()
        viewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                Bundle bundle = new Bundle();
                if (position == 0) {
                    return new SystemPager1Fragment().newInstance(bundle);
                } else {
                    return new SystemPager2Fragment().newInstance(bundle);
                }
            }

            @Override
            public int getCount() {
                return data.length;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                return super.instantiateItem(container, position);

            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                super.destroyItem(container, position, object);
            }
        });

        //viewPager滑动监听
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            /**
             * 当某个页面被选中的时候回调这个方法
             * @param position 被选中页面的位置
             */
            @Override
            public void onPageSelected(int position) {
                tabLayout.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
